package com.example.demo.rabbitMQ;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * <p>Description: </p>
 *
 * @author Eric Lee
 * @version v1.0.0
 * @since 2021/8/4 19:02
 **/

public class RabbitMqConnFactoryUtil {
    /**
     * 获取rabbit连接
     */
    public static Connection getRabbitConn() throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/");
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        Connection conn = factory.newConnection();

        //高级连接使用线程池
        // ExecutorService es = Executors.newFixedThreadPool(20);
        // Connection conn = factory.newConnection(es);

        return conn;
    }

    /**
     * 获取rabbit连接方式二
     */
    public static Connection getRabbitConn2() throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // 连接格式：amqp://userName:password@hostName:portNumber/virtualHost
        String uri = String.format("amqp://%s:%s@%s:%d%s", "admin", "admin",
                "127.0.0.1", 5672, "/");
        factory.setUri(uri);
        factory.setVirtualHost("/");
        Connection conn = factory.newConnection();
        return conn;
    }
}
